package Solution221_240;

public class Solution223 {


    public static int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
        int type = getType(A, B, C, D, E, F, G, H);
        int areaOne = (C - A) * (D - B);
        int areaTwo = (G - E) * (H - F);
        if (type == 2) {
            return areaOne;
        } else if (type == 3) {
            return areaTwo;
        } else if (type == 4) {
            return areaOne + areaTwo;
        } else {
            int areaThree = (C - E) * (H - B);
            return areaOne + areaTwo - areaThree;
        }
    }



    private static Integer getType(int A, int B, int C, int D, int E, int F, int G, int H) {
        int temp1X = E - A;
        int temp1Y = F - B;

        int temp2X = G - C;
        int temp2Y = H - D;

        if (temp1X > 0 && temp1Y > 0 && temp2X < 0 && temp2Y < 0) {
            return 2;
        }
        if (temp2X > 0 && temp2Y > 0 && temp1X < 0 && temp1Y < 0) {
            return 3;
        }
        if ((A >= G) || (C <= E) || (D <= F) || (B >= H)) {
            return 4;
        }
        return 1;
    }

    public static void main(String[] args) {
        System.out.println(computeArea(-2, -2, 2, 2, 1, 1, 3, 3));
    }
}
